/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		WinMain.cpp
//
//		Author			:		Jayson Wu   ( refrenced from Dave Brown's SGD Winmain)
//
//		Purpose			:		Sets up the window from which our game runs in
//
//		Dates			:		Written by: Jayson Wu
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////
//Start Pound including here
//===================================================================
#include <windows.h>	//To gain access to windows funcs/commands
#include "resource.h"
#include "CGame.h"

//Built in memory protection
#ifdef _DEBUG

#define _CRTDBG_MAP_ALLOC_
#include <stdlib.h>
#include <crtdbg.h>

#endif


//Windowed mode during debug automatically
#ifdef _DEBUG
const BOOL	g_bIS_WINDOWED			= TRUE;						
#else
const BOOL	g_bIS_WINDOWED			= FALSE;
#endif

const char* g_szWINDOW_CLASS_NAME	= "Tundurcatz";

const char* g_szWINDOW_TITLE		= "Hold The Line";
const int g_nWINDOW_WIDTH			= 800;
const int g_nWINDOW_HEIGHT			= 600;

LRESULT CALLBACK WindowProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	//Message Handler for system
	PAINTSTRUCT ps;
	HDC			hdc;

	switch(msg)
	{

	case WM_SYSKEYUP:
	case WM_SYSCHAR:
		return(0);
		break;

	case WM_CLOSE:
		{
			DestroyWindow(hWnd);
			PostQuitMessage(0);
		}
		break;

	case WM_ACTIVATE:
		{
			if( LOWORD(wParam) != WA_INACTIVE)
			{

			}
			else
			{

			}
		}
		break;

	case WM_CREATE:
		{
			return(0);
		}
		break;
	case WM_PAINT:
		{
			hdc = BeginPaint(hWnd,&ps);

			EndPaint( hWnd, &ps);
			return(0);
		}
		break;

	default:
		break;
	}
	return(DefWindowProc(hWnd, msg, wParam, lParam));
}

BOOL CheckIfAlreadyRunning()
{

	HWND hWnd = FindWindow(g_szWINDOW_CLASS_NAME, g_szWINDOW_TITLE);

	if( hWnd )
	{
		if( IsIconic(hWnd))

			ShowWindow(hWnd, SW_RESTORE);

		SetForegroundWindow(hWnd);

		return TRUE;
	}

	return FALSE;
}

BOOL RegisterWindowClass(HINSTANCE hInstance)
{

	WNDCLASSEX winClassEx;

	winClassEx.cbSize			=	sizeof(winClassEx);
	winClassEx.style			= CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
	winClassEx.lpfnWndProc		= WindowProc;
	winClassEx.cbClsExtra		= 0;
	winClassEx.cbWndExtra		= 0;
	winClassEx.hInstance		= hInstance;
	winClassEx.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
	winClassEx.hIconSm			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1));
	winClassEx.hCursor			= LoadCursor(NULL, IDC_ARROW);
	winClassEx.hbrBackground	= (HBRUSH)GetStockObject(BLACK_BRUSH);
	winClassEx.lpszMenuName		= NULL;
	winClassEx.lpszClassName	= g_szWINDOW_CLASS_NAME;

	return RegisterClassEx(&winClassEx);
}


HWND MakeWindow(HINSTANCE hInstance)
{

	DWORD dwWindowStyleFlags = WS_VISIBLE;

	if(g_bIS_WINDOWED)
	{
		dwWindowStyleFlags |= WS_OVERLAPPEDWINDOW;
	}
	else
	{
		dwWindowStyleFlags |= WS_POPUP;
		ShowCursor(FALSE);
	}
	//Lets set some windows
	RECT rWindow;
	SetRect( &rWindow, 0, 0, g_nWINDOW_WIDTH, g_nWINDOW_HEIGHT);
	AdjustWindowRectEx(&rWindow, dwWindowStyleFlags, FALSE, WS_EX_APPWINDOW);

	int nWindowWidth = rWindow.right - rWindow.left;
	int nWindowHeight = rWindow.bottom - rWindow.top;

	//Create the Window
	return CreateWindowEx( WS_EX_APPWINDOW,
		g_szWINDOW_CLASS_NAME,
		g_szWINDOW_TITLE,
		WS_OVERLAPPED | WS_MINIMIZEBOX | WS_SYSMENU/**/
		/*dwWindowStyleFlags*/,
		(int)((GetSystemMetrics(SM_CXSCREEN)*.5f) - (nWindowWidth*.5f)),
		(int)((GetSystemMetrics(SM_CYSCREEN)*.5f) - (nWindowHeight*.5f)),
		nWindowWidth, 
		nWindowHeight, 
		NULL, 
		NULL, 
		hInstance, 
		NULL);
}

///////////////////////////
//		WINMAIN				//
///////////////////////////

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine,
				   int nCmdShow)
{

	MSG msg;
	HWND hWnd;

#ifdef _DEBUG

	_CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
	_CrtSetBreakAlloc(-1);

#endif

	if( !hPrevInstance )
	{
		if( CheckIfAlreadyRunning())
			return FALSE;
	}

	//Register the window class
	if( !RegisterWindowClass(hInstance))
		return 0;

	//Create the Window
	hWnd = MakeWindow(hInstance);

	if( !hWnd )
		return 0;
	ShowWindow(hWnd, nCmdShow);
	UpdateWindow(hWnd);

	//////////////////////////////////////////////
	//////////////////////////////////////////////
	//		      Initialize Game here			 //
	//////////////////////////////////////////////
	//////////////////////////////////////////////
	CGame::GetInstance()->GameInit(hWnd, hInstance, g_nWINDOW_WIDTH, g_nWINDOW_HEIGHT, g_bIS_WINDOWED);
	//////////////////////////////////////////////
	//////////////////////////////////////////////
	while( TRUE )
	{

		if( PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
		{
			if( msg.message == WM_QUIT)
				break;
			TranslateMessage(&msg);

			DispatchMessage(&msg);
		}

		////////////////////////////////////
		//			Game Logic
		////////////////////////////////////
		if( CGame::GetInstance()->Main() == false )
			PostQuitMessage(0);
	}
	/////////////////////////////////////
	//	ShutDown the Game
	/////////////////////////////////////

	CGame::GetInstance()->ShutDown();

	UnregisterClass( g_szWINDOW_CLASS_NAME, hInstance);

	//Return to Windows
	return (int)(msg.wParam);

}